//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<UiManager.cpp>
///	@path	~/src/appear/xwin/
///	@date	2008/09/09
///	@brief	.

#define _FULLAPI
#include "config/config.h"

#include "lib/utilities/observer.h"

#include "graphics/ITechnique.h"
#include "graphics/IShaderVariable.h"

#include "gui/composite/composition.h"

#include "appear/xwin/UiManager.h"

namespace xeres {

	// UiManager
	UiManager::UiManager( void )
	{

	}

	// ~UiManager
	UiManager::~UiManager( void )
	{

	}

	namespace
	{
		struct _DesktopCanvasVertex
		{
			FLOAT x, y, z, rhw;
			float u , v;
		};
	}

	// Initialize
	void UiManager::Initialize( void * window , InputDispatcher * dispatcher , Engine& engine )
	{
		TRACK_FUNCTION_SCOPE();
		
		if( m_desktop.IsValid() )
		{
			TRACE_ERROR( _S("UiManager::Initialize: UI Manager has been initialized.") );
			return;
		}

		Ref<IEffect> effect = engine.LoadEffect( _S("ui/frame_composition.fx") , NULL , false );

		m_desktop = new XAppDesktop( &engine );
		m_dispatcher = dispatcher;
 		m_desktop->Initialize( engine , effect );

		int width = engine.ResX<int>();
		int height = engine.ResY<int>();

		// Resize desktop.
		OnSize( engine , Pair<int>(width,height) );
	}

	// Finalize
	void UiManager::Finalize( void )
	{
		TRACK_FUNCTION_SCOPE();

		if( m_desktop.IsValid() )
		{
			m_desktop->OnClose();
			m_desktop->Finalize();
			m_desktop = NULL;
		}
	}

	// Tick
	void UiManager::Tick( const Frame& frame )
	{
		TRACK_FUNCTION_SCOPE();

		COUNTER_GUARD( _S("ui") );
		if( m_desktop.IsValid() && m_desktop->GetVisible() )
		{
			Updater updater( m_desktop );
			m_desktop->Tick( updater , frame.dtime() );
		}
	}

	// Render
	void UiManager::Render( RenderContext& rc )
	{
		TRACK_FUNCTION_SCOPE();

		COUNTER_GUARD( _S("ui") );
		if( m_desktop.IsValid() && m_desktop->GetVisible() )
		{
			m_desktop->Draw( rc );
		}
	}

	// OnSize
	void UiManager::OnSize( Engine& engine , const Pair<int>& size )
	{
		TRACK_FUNCTION_SCOPE();
		if( m_desktop.IsValid() && ( size != m_desktop->GetDesktopSize() ) )
		{
			m_desktop->SetAppDesktopSize( engine , size );
			m_desktop->Resize( engine , size );
			m_desktop->RedrawAll();
		}
	}

	// OnKeyInput
	HandleInput UiManager::OnKeyInput( const KeyInput& input )
	{
		TRACK_FUNCTION_SCOPE();
		if( m_desktop.IsValid() && m_desktop->GetEnable() && m_desktop->GetVisible() )
		{
			Updater updater( m_desktop , input );
			// Reset local point of updater.
			updater.ResetPoint();

			return m_desktop->OnKeyInput( updater );
		}
		return INPUT_UNHANDLED; 
	}

	// OnMouseInput
	HandleInput UiManager::OnMouseInput( const MouseInput& input )
	{
		TRACK_FUNCTION_SCOPE();
		if( m_desktop.IsValid() && m_desktop->GetEnable() && m_desktop->GetVisible() )
		{
			Updater updater( m_desktop , input );
			// Reset local point of updater.
			updater.ResetPoint();

			return m_desktop->OnMouseInput( updater );
		}
		return INPUT_UNHANDLED;
	}
	// ToggleDesktop
	void UiManager::ToggleDesktop( void )
	{
		if( m_desktop.IsValid() )
		{
			m_desktop->SetVisible( !m_desktop->GetVisible() );
		}
	}
} // namespace xeres
